"""
Case Type   : ustore copy from/to
Case Name   : ustore-copy compatible_illegal_chars参数
Create At   : 2024-07-25
Owner       : songjing20
Description :
    1.创建ustore表并插入数据
    2.构造数据文件
    3.执行copy from，指定compatible_illegal_chars参数
    4.清理环境
Expect      :
    1.执行成功
    2.成功
    3.copy成功
    4.成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class UstoreCopy(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.node_pri = Node('PrimaryDbUser')
        self.constant = Constant()
        self.tb_name = 't_copy_0014'
        self.file_abspath = os.path.join(
            macro.DB_BACKUP_PATH, f"{self.tb_name}.txt")

    def test_copy_from_to(self):
        text = '-----step1:创建ustore表，插入数据;expect:成功-----'
        self.log.info(text)
        sql = f'''drop table if exists {self.tb_name} cascade;
        create table {self.tb_name}(a integer,b char(16),name varchar(20))
        with(storage_type=ustore);'''
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res,
                      f'执行失败:{text}')

        text = '-----step2:构造数据文件;expect:成功-----'
        self.log.info(text)
        cmd = f'echo "001\tsk1\ttt1\n001\tsk1\ttt2\n' \
              f'001\tsk1\ttt3" > {self.file_abspath};' \
              f'cat {self.file_abspath};'
        self.log.info(cmd)
        cmd_res = self.node_pri.sh(cmd).result()
        self.log.info(cmd_res)

        text = '-----step3:执行copy from，指定compatible_illegal_chars参数;' \
               'expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execut_db_sql(
            f"copy {self.tb_name} from '{self.file_abspath}' "
            f"compatible_illegal_chars;"
            f"select * from {self.tb_name} order by 1;")
        self.log.info(res)
        self.assertIn('COPY 3', res, f'执行失败:{text}')
        self.assertIn('1 | sk1              | tt1\n '
                      '1 | sk1              | tt2\n '
                      '1 | sk1              | tt3\n(3 rows)',
                      res, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step4:清理环境;expect:成功-----'
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            f"drop table if exists {self.tb_name} cascade;")
        self.log.info(drop_res)
        cmd = f'rm -f {self.file_abspath};echo $?;'
        self.log.info(cmd)
        cmd_res = self.node_pri.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, drop_res,
                      f'删表失败:{text}')
        self.assertEqual('0', cmd_res, f'执行失败:{text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
